/**
 * FileName: NursesController
 * Author:   P
 * Date:     2021/4/8 17:57
 * Description:
 */
package com.hospital.nurse.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.nurse.pojo.Nurses;
import com.hospital.nurse.service.NursesService;
import com.hospital.nurse.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/nurses")
public class NursesController {
    @Autowired
    private NursesService nursesService;



    @RequestMapping("/login")
    public Map<String,Object> nurseLogin(Nurses user){
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<Nurses> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        Nurses nurse = nursesService.getOne(queryWrapper);
        if (nurse==null){
            map.put("msg","没有该用户");
            return map;
        }
        if (nurse!=null && !nurse.getPassword().equals(user.getPassword())) {
            map.put("msg","密码错误");
            return map;
        }
        if (nurse.getPassword().equals(user.getPassword())){
           Map<String, String>  payload= new HashMap<>();
           payload.put("nid",""+user.getNId());
           payload.put("did",""+user.getDId());
           payload.put("username",""+user.getUsername());
           payload.put("nname",""+user.getNName());
           String token = JWTUtils.getToke(payload);
           map.put("token",token);
           map.put("msg","登录成功");
           return map;
        }
         map.put("msg","未知错误");
        return map;
    }
}
